/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.simple.core.enums;

import cn.hutool.core.util.StrUtil;
import com.simple.core.exception.BizException;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author mocun
 * @since : 2023/3/16
 */

@Getter
public enum ResponseCode {

    // 200 成功
    SUCCESS(HttpStatus.OK.value(), "操作成功"),

    // 404
    NOT_FOUND(HttpStatus.NOT_FOUND.value(), "对象不存在"),

    // 500 异常
    FAIL(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器异常"),


    // NOTE：请看我请看我请看我请看我请看我请看我请看我请看我请看我请看我请看我请看我请看我
    // 10000 开始业务代码异常，前面预留

    // 10001-10100 用户登录相关
    NOT_LOGIN(10001, "用户未登录"),
    TOKEN_EXPIRED(10002, "登录凭证已过期"),
    TOKEN_ERROR(10003, "登录凭证错误"),
    NOT_BIND_COMMUNITY(10004, "用户未绑定社区"),
    USER_NOT_EXISTS(10005, "用户不存在"),
    USER_BEEN_BANNED(10005, "当前用户已被封禁"),

    // 10101-10200 微信小程序相关
    SECURITY_MSG(10101, "内容检测异常"),
    SECURITY_MEDIA(10102, "音视频检测异常"),


    // 10201-....下面自由发挥
    NO_PERMISSION(10201, "无权限"),
    BUSY(10202, "系统繁忙"),
    TAKEDOWN(10203, "内容已下架"),
    ;

    private final int code;
    private final String message;

    ResponseCode(final int code, final String message) {
        this.code = code;
        this.message = message;
    }

    public static ResponseCode of(int code) {
        Optional<ResponseCode> first = Arrays.stream(ResponseCode.values()).filter(r -> r.getCode() == code).findFirst();
        if (first.isPresent()) {
            return first.get();
        }
        throw new BizException(StrUtil.format("code={}未定义", code));
    }
}
